package com.wuneng.commonlibrary.net;

import android.content.Context;
import android.os.Environment;
import android.widget.Toast;

import com.wuneng.commonlibrary.util.JsonUtils;
import com.wuneng.commonlibrary.util.NetUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.FileCallBack;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zhy.http.okhttp.request.RequestCall;

import java.io.File;
import java.util.Map;

import okhttp3.Call;
import okhttp3.MediaType;


/**
 * 描述:okhttp的工具类
 * Created by 汤家骏 on 2017/4/6.
 */

public class OkNetUtil {
    /**
     * 普通的get请求
     *
     * @param context      上下文
     * @param url          访问的地址
     * @param params       传的参数
     * @param httpCallBack 接口回调结果
     */
    public static void get(Context context, String url, Map params, final OkHttpCallBack httpCallBack,int id) {
        if (NetUtils.isNetworkConnected(context)) {
            OkHttpUtils

                    .get()
                    .id(id)
                    .url(url)
                    .params(params)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            httpCallBack.error(call, e, id);
                        }

                        @Override
                        public void onResponse(String response, int id) {
                            ActionResponse actionResponse = JsonUtils.getObj(response,ActionResponse.class);
                            httpCallBack.response(actionResponse, id);
                        }
                    });
        }else{
            httpCallBack.onNoNetWork(id);
            return;
        }
    }

    /**
     * 普通的post请求
     *
     * @param context      上下文
     * @param url          访问的地址
     * @param params       传的参数
     * @param httpCallBack 接口回调结果
     */
    public static void postNoHeader(Context context, String url, Map params, final OkHttpCallBack httpCallBack,int id) {


        if (NetUtils.isNetworkConnected(context)) {
            OkHttpUtils
                    .post()
                    .id(id)
                    .url(url)
                    .params(params)

                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {

                            httpCallBack.error(call, e, id);
                        }

                        @Override
                        public void onResponse(String response, int id) {
                            ActionResponse actionResponse = JsonUtils.getObj(response,ActionResponse.class);
                            httpCallBack.response(actionResponse, id);
                        }
                    });
        }else {
            httpCallBack.onNoNetWork(id);
            return;

        }
    }

    /**
     * 普通的post请求 带header
     *
     * @param context      上下文
     * @param url          访问的地址
     * @param params       传的参数
     * @param httpCallBack 接口回调结果
     */
    public static void post(Context context, String url, Map params, Map<String,String> headers,final OkHttpCallBack httpCallBack,int id) {
        if (NetUtils.isNetworkConnected(context)) {
            OkHttpUtils
                    .post()
                    .id(id)
                    .url(url)
                    .params(params)
                    .headers(headers)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            httpCallBack.error(call, e, id);
                        }

                        @Override
                        public void onResponse(String response, int id) {
                            ActionResponse actionResponse = JsonUtils.getObj(response,ActionResponse.class);
                            httpCallBack.response(actionResponse, id);
                        }
                    });
        }else {
            httpCallBack.onNoNetWork(id);
            return;
        }
    }

    /**
     * post方法上传文件
     *
     * @param context      上下文
     * @param url          访问的地址
     * @param filePath     上传的文件的路径
     * @param httpCallBack 接口回调结果
     */
    public static void upFile(Context context, String url, String filePath, final OkHttpUpFileCallBack httpCallBack,int id) {
        if (NetUtils.isNetworkConnected(context)) {
            File file = new File(Environment.getExternalStorageDirectory(), filePath);
            if (!file.exists()) {
                Toast.makeText(context, "文件不存在，请修改文件路径", Toast.LENGTH_SHORT).show();
                return;
            }
            OkHttpUtils
                    .postFile()
                    .url(url)
                    .file(file)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void inProgress(float progress, long total, int id) {
                            httpCallBack.process(progress, total, id);
                        }

                        @Override
                        public void onError(Call call, Exception e, int id) {
                            httpCallBack.error(call, e, id);
                        }

                        @Override
                        public void onResponse(String response, int id) {
                            httpCallBack.fileResponse(response, id);
                        }
                    });

        }else{
            httpCallBack.onNoNetWork(id);
            return;
        }
    }


    public static void upFileWithFormData(Context context, String url, String fileName,String name,File file,Map<String,String> params, Map<String,String> headers,final OkHttpUpFileCallBack httpCallBack,int id) {
        if (NetUtils.isNetworkConnected(context)) {

            OkHttpUtils
                    .post()
                    .addFile(name,fileName,file)
                    .params(params)
                    .headers(headers)
                    .url(url)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void inProgress(float progress, long total, int id) {
                            httpCallBack.process(progress, total, id);
                        }

                        @Override
                        public void onError(Call call, Exception e, int id) {
                            httpCallBack.error(call, e, id);
                        }

                        @Override
                        public void onResponse(String response, int id) {
                            httpCallBack.fileResponse(response, id);
                        }
                    });

        }else{
            httpCallBack.onNoNetWork(id);
            return;
        }
    }
    /**
     *
     * @param context   上下文
     * @param url       需要访问的地址
     * @param content   上传的内容
     * @param callBack  接口回调
     */
    public static void postString(final Context context, String url, String content, final OkHttpCallBack callBack, final int id) {
        if (NetUtils.isNetworkConnected(context)) {
            OkHttpUtils
                    .postString()
                    .url(url)
                    .id(id)
                    .mediaType(MediaType.parse("application/json; charset=utf-8")) //设置post的字符串为json字符串并设置编码
                    .content(content) //用Gson将User对象转化为Json字符串的形式作为content
                    .build()

                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            callBack.error(call,e,id);
                        }

                        @Override
                        public void onResponse(String response, int id) {

                            ActionResponse actionResponse = JsonUtils.getObj(response,ActionResponse.class);
                            callBack.response(actionResponse,id);
                        }
                    });



        }

    }



    /**
     * 下载文件
     *
     * @param context      上下文
     * @param url          地址
     * @param fileName     下载的文件的名称 貌似是自己定义的
     * @param httpCallBack 接口回调
     */
    public static void downFile(Context context, String url, String path,String fileName, final OkHttpDownFileCallBack
            httpCallBack,int id) {
        if (NetUtils.isNetworkConnected(context)) {
            OkHttpUtils
                    .get()
                    .url(url)
                    .id(id)
                    .build()

                    .execute(new FileCallBack(path, fileName) {

                        @Override
                        public void inProgress(float progress, long total, int id) {
                            httpCallBack.process(progress, total, id);
                        }

                        @Override
                        public void onError(Call call, Exception e, int id) {
                            httpCallBack.error(call, e, id);
                        }

                        @Override
                        public void onResponse(File response, int id) {
                            httpCallBack.fileResponse(response, id);
                        }
                    });
        }else{
           httpCallBack.onNoNetWork(id);
            return;
        }
    }

    public static RequestCall downFileCanCancel(Context context, String url, String path, String fileName, final OkHttpDownFileCallBack
            httpCallBack,int id) {
        RequestCall requestCall = null;
        if (NetUtils.isNetworkConnected(context)) {
            requestCall = OkHttpUtils
                    .get()
                    .url(url)
                    .id(id)
                    .build();
            requestCall.execute(new FileCallBack(path, fileName) {

                @Override
                public void inProgress(float progress, long total, int id) {
                    httpCallBack.process(progress, total, id);
                }

                @Override
                public void onError(Call call, Exception e, int id) {
                    httpCallBack.error(call, e, id);
                }

                @Override
                public void onResponse(File response, int id) {
                    httpCallBack.fileResponse(response, id);
                }
            });
        } else {
            httpCallBack.onNoNetWork(id);
            return requestCall;
        }

        return requestCall;
    }

    /**
     * 取消okhttp网络请求,此方法在ondestroy里面调用
     *
     * @param context
     */
    public static void cancleOkNet(Context context) {
        OkHttpUtils.getInstance().cancelTag(context);
    }


    /**
     * 普通的回调接口
     */
    public interface OkHttpCallBack {
        void onNoNetWork(int id);

        void error(Call call, Exception e, int id);

        void response(ActionResponse response, int id);
    }

    /**
     * 下载文件
     */
    public interface OkHttpDownFileCallBack {
        void onNoNetWork(int id);

        void process(float progress, long total, int id);

        void error(Call call, Exception e, int id);

        void fileResponse(File response, int id);
    }

    /**
     * 上传文件
     */
    public interface OkHttpUpFileCallBack {
        void onNoNetWork(int id);

        void process(float progress, long total, int id);

        void error(Call call, Exception e, int id);

        void fileResponse(String response, int id);
    }

    /**
     * 上传String的回调接口
     */
    public interface OkHttpPostStringCallBack {

        void onFailure(Call call, Exception e);

        void onResponse(Call call, String response);
    }




}
